import weui from 'weui.js'
// 是否为盈家生活安卓端
const isAndroidApp = () =>
  window.navigator.userAgent.indexOf('innjiaAppAndroid') > -1;

// 是否为盈家生活iOS端
const isIOSApp = () =>
  window.navigator.userAgent.indexOf('innjiaAppIos') > -1;

// 返回App界面，不使用Vue-router
const backToApp = () => {
  if (isAndroidApp()) {
    androidAndjs.backToApp();
  } else if (isIOSApp()) {
    window.webkit.messageHandlers.backToApp.postMessage('');
  }
};

// 跳转App端的编辑租约页面
const editRent = () => {
  if (isAndroidApp()) {
    androidAndjs.editRent();
  } else if (isIOSApp()) {
    window.webkit.messageHandlers.editRent.postMessage('');
  }
};

// 跳转App端的IM
const toIM = () => {
  if (isAndroidApp()) {
    androidAndjs.toIM();
  } else if (isIOSApp()) {
    window.webkit.messageHandlers.toIM.postMessage('');
  }
};

// App内嵌支付
// 参数如下:
// title: (String)
// totalFee: (String)
// bill_no: (String)
// quantity: (String)
// body: (String)
// type: (String) 0代表支付宝；1代表微信
const toPay = (params, successCallback, errorCallback) => {
  if (isAndroidApp()) {
    const {
      title,
      totalFee,
      bill_no,
      quantity,
      body,
      type
    } = params;
    androidAndjs.toPay(title, totalFee, bill_no, quantity, body, type);

  } else if (isIOSApp()) {
    window.webkit.messageHandlers.toPay.postMessage(params);
  }

  // 注入支付回调
  window.paySuccess = successCallback;
  window.payFailed = errorCallback;
};

export default {
  backToApp,
  editRent,
  toIM,
  toPay
};